$(function(){
	$("#phone").blur(function(){
		var re= /^1(3|5|7|8)[0-9]{9}/;
		if(re.test($(this).val()) == false || $(this).val() == ""){
			$(".tishi").eq(0).show();
			return
		}else{
			$(".tishi").eq(0).hide();
		}				
	})
	$("#password").blur(function(){
			var re= /[a-z0-9]{6,16}/
			if(re.test($(this).val()) ==false || $(this).val() == ""){
				$(".tishi").eq(2).show()
			}else{
				$(".tishi").eq(2).hide();
			}
		})
	$("#affirm").blur(function(){
		
		if($(this).val() != $("#password").val()){
			$(".tishi").eq(3).show();
		}else{
			$(".tishi").eq(3).hide();
		}
	})
	$("#register").click(function(){
		re = /^1(3|5|7|8)[0-9]{9}/
		var pw = /[a-z0-9]{6,16}/
		var usn = $("#phone").val()
		var pwd = $("#password").val()
		if(!usn){
			alert("手机号不能为空！")
			return
		}
		if(re.test(usn) == false){
			alert("请输入正确的手机号")
			return
		}
		if(pw.test(pwd) == false){
			alert("请输入6-16位字符")
			return
		}
		if(!pwd){
			alert("密码不能为空!")
			return
		}
		var users = getCookie("register") ?getCookie("register") :"";
		users = convertStrToObj(users);
		if(usn in users){
			alert("用户名已被注册！")
			return
		}else{
			users[usn] = pwd;
			userStr = convertObjToStr(users);
			setCookie("register",userStr,7);
			console.log(decodeURIComponent(document.cookie))
		     alert("注册成功！");
		   location.href = "##"
		}
	})
	function convertStrToObj(str){
		if(!str){
			return {}
		}
		var users = str.split(":")
		var obj = {}
		for(var i = 0; i < users.length; i++){
			var userData = users[i].split(",")
			obj[userData[0]] = userData[1]
		}
		return obj
	}
	function convertObjToStr(obj){
		var str = "";
		for(var usn in obj){
			var pwd = obj[usn];
			if(str){
				str +=":";
			}
			str += usn + "," +pwd;
		}
		return str;
	}
	
})
